<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();

        $this->load->library('template');
        $this->template->set_template('backend');
        $this->load->library('carabiner');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->model('auth_model');
    }
   
    
    function login() {

        
        // Already loged in
        if (is_loggedin()) {
            redirect('backend/dashboard');            
            return TRUE;
        }
        
        $this->render_data['redirect'] = $this->input->get('redirect');

        // showinh login page
        if (!count($_POST)) {
            
           
            $this->template->write_view('content', 'backend/login',$this->render_data);
            $this->template->render();
            
            return TRUE;
        }
        
        
        $this->form_validation->set_rules('email', 'Email', 'trim|required|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|required');
        if ($this->form_validation->run() == FALSE) {
            $errors = validation_errors();
            $this->render_data['error'] = $errors;
            $this->template->write_view('content', 'backend/login', $this->render_data);
            $this->template->render();
        } else {
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $row = $this->auth_model->login($email, $password);

            
            if ($row) {

                if ($this->input->post('remember')) {
                    $this->load->library('encrypt');
                    $cookie = array(
                        'name' => 'remember',
                        'value' => $this->encrypt->encode($row['member_id']),
                        'expire' => 86500,
                    );

                    $this->input->set_cookie($cookie);
                }

                $sess_array = array(
                    'member_id' => $row['member_id'],
                    'member_email' => $row['member_email'],
                    'member_firstname' => $row['member_firstname'],
                    'member_lastname' => $row['member_lastname'],
                    'member_role' => $row['member_role'],
                    'member_thumb' => $row['member_thumb']
                );
                $this->session->set_userdata('is_loggedin', $sess_array);
                
                if ($this->render_data['redirect'] != '')
                {
                    redirect(base64_decode($this->input->get('redirect')));
                }
                else
                {
                    redirect($this->router->reverseRoute('backend'));
                }
            } else {

                $this->render_data['error'] = 'Invalid Username or Password';
                $this->template->write_view('content', 'backend/login',$this->render_data);
                $this->template->render();
            }
        }
    }

    function logout() {
        $this->load->helper('cookie');
        delete_cookie('remember');
        $this->auth_model->logout();
        redirect($this->router->reverseRoute('backend'));
        return TRUE;
    }

}
